import { PrismaService } from '@app/prisma';
import { TaskItemEntity } from '@app/prisma/task.item.entity/task.item.entity';
import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
    constructor(
        private prisma: PrismaService
    ) { }

    async createTaskOnUser(
        userId: number,
        teacherId: number,
        TaskId: number,
        common: string = "",
        items: TaskItemEntity[],
        form: {
            [k in number]: number
        }
    ) {
        await this.prisma.userOnTask.create({
            data: {
                user: {
                    connect: {
                        id: userId
                    }
                },
                teacher: {
                    connect: {
                        id: teacherId
                    }
                },
                common,
                task: {
                    connect: {
                        id: TaskId
                    }
                }
            }
        })
        await this.prisma.userTaskItem.createMany({
            data: items.map(({ id }) => {
                return {
                    taskItemId: id,
                    userOnTaskUserId: userId,
                    userOnTaskTaskId: TaskId,
                    userOnTaskTeacherId: teacherId,
                    score: form[id]
                }
            })
        })
    }
}
